/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserManageList.java
 *
 * Created on 2009-12-5, 23:29:05
 */

package com.ordering.menu;

import com.ordering.dao.UserManageJpaController;
import com.ordering.dao.exceptions.NonexistentEntityException;
import com.ordering.pojo.UserManage;
import com.ordering.utils.WindowLocationUtils;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author hua
 */
public class UserManageList extends javax.swing.JDialog {

    /** Creates new form UserManageList */
    public UserManageList(java.awt.Frame parent, boolean modal) {
         super(parent, modal);
        initComponents();
        WindowLocationUtils.hideColumn(userTable, 3);
        WindowLocationUtils.setWindowCenter(this);
        initTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        userTable = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        btnAddNew = new javax.swing.JMenu();
        btnEdit = new javax.swing.JMenu();
        btnDel = new javax.swing.JMenu();
        btnRefresh = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("用户管理");

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        userTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "用户名", "备注", "是否有效", "序号"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Long.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        userTable.setName("userTable"); // NOI18N
        userTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(userTable);
        userTable.getColumnModel().getColumn(0).setPreferredWidth(120);
        userTable.getColumnModel().getColumn(1).setPreferredWidth(200);

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        btnAddNew.setText("新增");
        btnAddNew.setName("btnAddNew"); // NOI18N
        btnAddNew.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnAddNewMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnAddNew);

        btnEdit.setText("修改");
        btnEdit.setName("btnEdit"); // NOI18N
        btnEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnEditMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnEdit);

        btnDel.setText("删除");
        btnDel.setName("btnDel"); // NOI18N
        btnDel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnDelMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnDel);

        btnRefresh.setText("刷新");
        btnRefresh.setName("btnRefresh"); // NOI18N
        btnRefresh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnRefreshMouseClicked(evt);
            }
        });
        jMenuBar1.add(btnRefresh);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * 监听编辑窗口关闭后刷新列表事件
     */
    public void childWinClosed(){
        initTable();
    }
    private void btnAddNewMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddNewMouseClicked
         UserManageEdit editWin = new UserManageEdit();
         editWin.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosed(java.awt.event.WindowEvent e) {
                        childWinClosed();
                    }
                });
         editWin.setVisible(true);
    }//GEN-LAST:event_btnAddNewMouseClicked

    private void btnEditMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEditMouseClicked
       int rowIndex = userTable.getSelectedRow();
        if(rowIndex==-1)
           JOptionPane.showMessageDialog(this, "请选择行");
        else{
           Object vipId = ((DefaultTableModel)userTable.getModel()).getValueAt(rowIndex,3);
           if(vipId!=null){
                UserManageJpaController control = new UserManageJpaController();
                UserManage user = control.findUserManage((Long)vipId);
                UserManageEdit editWin = new UserManageEdit(user);
                editWin.addWindowListener(new java.awt.event.WindowAdapter() {
                            public void windowClosed(java.awt.event.WindowEvent e) {
                                childWinClosed();
                            }
                       });
                editWin.setVisible(true);
           }
        }
    }//GEN-LAST:event_btnEditMouseClicked

    private void btnDelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDelMouseClicked
         int rowIndex = userTable.getSelectedRow();
        if(rowIndex==-1)
           JOptionPane.showMessageDialog(this, "请选择行");
        else{
           Object userId = ((DefaultTableModel)userTable.getModel()).getValueAt(rowIndex,3);
           if(userId!=null&&!"".equalsIgnoreCase(userId.toString())){
               UserManageJpaController control = new UserManageJpaController();
                try {
                    control.destroy((Long)userId);
                    ((DefaultTableModel)userTable.getModel()).removeRow(rowIndex);
                } catch (NonexistentEntityException ex) {
                    Logger.getLogger(VipManageList.class.getName()).log(Level.SEVERE, null, ex);
                }
           }
        }
    }//GEN-LAST:event_btnDelMouseClicked

    private void btnRefreshMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnRefreshMouseClicked
        initTable();
    }//GEN-LAST:event_btnRefreshMouseClicked
    public void initTable(){
        for(int rowIndex=userTable.getRowCount()-1;rowIndex>-1;rowIndex--){
            ((DefaultTableModel)userTable.getModel()).removeRow(rowIndex);
        }
        UserManageJpaController control = new UserManageJpaController();
        List<UserManage> users = control.findUserManageEntities();
        Object[] rowData = new Object[userTable.getColumnCount()] ;
        for(UserManage user:users){
            rowData[0] = user.getUserid();
            rowData[1] = user.getPuis();
            rowData[2] = user.getDelmark()?"有效":"无效";
            rowData[3] = user.getPk();
            ((DefaultTableModel)userTable.getModel()).addRow(rowData);
        }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                UserManageList dialog = new UserManageList(new javax.swing.JFrame(), false);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu btnAddNew;
    private javax.swing.JMenu btnDel;
    private javax.swing.JMenu btnEdit;
    private javax.swing.JMenu btnRefresh;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable userTable;
    // End of variables declaration//GEN-END:variables

}
